<?php
/**
* The lazy load extension,
* Reference here : {@link http://www.appelsiini.net/projects/lazyload}
*         <script>
             
             //$(window).bind("load", function() { 
//                 var timeout = setTimeout(function() { $("img").trigger("sporty") }, 5000);
//             });     
        </script>
*/
class FLazyLoad extends CWidget
{
    public $id='';
    public $target='img';
    public $config=array();
    public $src='';
    public $dataOriginal='';
    
    public function init(){
        parent::init();
        $this->publishAssets();
    }
    
    public function run()
    {
        $config = CJavaScript::encode($this->config);
        Yii::app()->clientScript->registerScript($this->getId(), "
            $('$this->target').lazyload($config);
        ");
    }

    public function publishAssets()
    {
        $assets = dirname(__FILE__).'/assets';
        $baseUrl = Yii::app()->assetManager->publish($assets);
        if(is_dir($assets)){
            Yii::app()->clientScript->registerCoreScript('jquery');
            Yii::app()->clientScript->registerScriptFile($baseUrl . '/jquery.lazyload.js', CClientScript::POS_HEAD);
        
        } else {
            throw new Exception('FLazyLoad - Error: Couldn\'t find assets to publish.');
        }
    }
}
?>
